package com.hotleproject.controller;

import com.hotleproject.pojo.Comment;
import com.hotleproject.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
//@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@RequestMapping("/users")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @RequestMapping("/addComment")
    public String addUsers(HttpServletResponse response, Comment comment, HttpServletRequest request, Model model) throws IOException {
//        将前端传过来的数据用Comment类进行封装
        //可以获取到封装后的值
//        model.addAttribute("w",comment.getCommentDate());
//        model.addAttribute("x",comment.getHotlename());
//        model.addAttribute("y",comment.getUsername());
        //将封装的东西进行数据库操作
        this.commentService.addComment(comment);
//        return "text";
//        response.setContentType("text/html;charset=gb2312");
//        PrintWriter out = response.getWriter();
//        String hotlename=request.getParameter("hotlename");
//        out.print("<script language=\"javascript\">alert('注册成功，返回登录界面！');window.location.href='/users/singleDeal?hotlename='</script>");
        model.addAttribute("hotlename",request.getParameter("hotlename"));
//            return "addCommentToSingleDeal";
        return "redirect:singleDeal?hotlename="+request.getParameter("hotlename").toString();
    }
//    findHotleCommentPage
    @RequestMapping("/findHotleCommentPage")
    public String findHotleCommentPage(HttpSession session, HttpServletResponse response, HttpServletRequest request, Model model) throws IOException {
        String hotlename=session.getAttribute("adminis").toString();
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 15;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count =  this.commentService.getCommentCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? 1 : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;


////        String hotlename = session.getAttribute("adminis").toString();
//        model.addAttribute("hotlename", hotlename);
//////        model.addAttribute("pageNo1", pageNo);
//        model.addAttribute("pageNo", tempPageNo);
//        model.addAttribute("pageSize", pageSize);
//        return "text";
        List<Comment> list=this.commentService.findHotleComment(hotlename);
//
//        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list",list);
//        model.addAttribute("list",list);
        model.addAttribute("pageNo",pageNo);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("maxPage",maxPage);
        return "comment_data";

//        return "text";
    }

    @RequestMapping("/deleteComment")
    public String deleteComment(HttpSession session, HttpServletRequest request, Model model) throws IOException {
        String username=session.getAttribute("users").toString();
        this.commentService.deleteComment(username,request.getParameter("commentDate"));
        return "redirect:/users/UserCount";
//        return "text";
    }



}
